/*
 * Copyright (C) 2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;
using DCSharp.Backend.Protocols;
using DCSharp.Settings;

namespace DCSharp
{
	public class ProtocolHelper : IHubProtocolHelper, IUserProtocolHelper
	{
		private Runtime runtime;

		public ProtocolHelper(Runtime runtime)
		{
			if (runtime == null)
			{
				throw new ArgumentNullException("runtime");
			}
			this.runtime = runtime;
		}

		#region IHubProtocolHelper

		public IConnectionSettings ConnectionSettings
		{
			get { return runtime.ConnectionSettings; }
		}

		public int Slots
		{
			get { return runtime.Settings.UploadSlots ?? int.MaxValue; }
		}

		public int FreeSlots
		{
			get { return runtime.UploadManager.FreeSlots; }
		}

		public long ShareTotal
		{
			get { return runtime.ShareManager.Total; }
		}

		public UserConnection CreateUserConnection(IPEndPoint endPoint,
			ConnectionProtocol protocol)
		{
			return runtime.ConnectionManager.CreateConnectionTo(endPoint,
				protocol);
		}

		public bool CanSearch(string identifier)
		{
			// TODO: Handle search floods.
			return true;
		}

		public SearchResult[] Search(SearchInfo searchInfo, int? maxResults)
		{
			return runtime.ShareManager.Search(searchInfo, maxResults);
		}

		#endregion

		#region IUserProtocolHelper

		public TransferDirection GetTransferDirection(User user)
		{
			return runtime.DownloadManager.IsInterestedIn(user) ?
				TransferDirection.Down : TransferDirection.Up;
		}

		#endregion

		#region ISearchResultHandler

		public void HandleSearchResult(SearchResult result)
		{
			// The TTH is required.
			if (result.Type == ResultType.File && result.TTH == null)
			{
				return;
			}
			runtime.SearchManager.HandleIncomingResult(result);
		}

		#endregion

		#region ILegacyUserIdentifier

		public bool IdentifyUser(string nick, string hubName, string hubAddress,
			out User user, out HubConnection hub)
		{
			hub = null;
			user = null;

			if (hubAddress != null)
			{
				runtime.HubManager.TryGetHub(hubAddress, out hub);
			}
			if (hub == null && hubName != null)
			{
				hub = runtime.HubManager.Find(delegate(HubConnection h)
				{
					return h.Name == hubName;
				});
			}
			if (hub == null && nick != null &&
				IdentifyFromNick(nick, out user, out hub))
			{
				return true;
			}
			else if (hub != null)
			{
				Identity identity = hub.Users.Find(delegate(Identity i)
				{
					return i.Nick == nick;
				});
				user = identity != null ? identity.User : null;
			}
			return user != null;
		}

		private bool IdentifyFromNick(string nick, out User user,
			out HubConnection hub)
		{
			user = null;
			hub = null;

			lock (runtime.HubManager.SyncRoot)
			{
				foreach (HubConnection h in runtime.HubManager)
				{
					Identity identity = h.Users.Find(delegate(Identity i)
					{
						return i.Nick == nick;
					});
					if (identity != null)
					{
						user = identity.User;
						hub = h;

						if (runtime.DownloadManager.IsInterestedIn(user))
						{
							return true;
						}
					}
				}
			}
			return user != null;
		}

		#endregion
	}
}
